/*
** symList.c for symList.c in /home/vermel/local/epitech/tek2/c/ftrace/pullup-project/sources
** 
** Made by nathan vermelle
** Login   <vermel_n@epitech.net>
** 
** Started on  Sun Jul  1 17:40:23 2012 nathan vermelle
** Last update Sun Jul  1 17:40:25 2012 nathan vermelle
*/

#include <string.h>
#include <stdlib.h>
#include "../includes/elf.h"

static t_symList	*addSymlist(t_symList *l, Elf64_Sym *s, char *n)
{
  t_symList	*new;

  new = xmalloc(sizeof(t_symList));
  new->addr = s->st_value;
  new->str = strdup(n);
  new->next = l;
  return (new);
}

void		deleteSymList(t_symList *l)
{
  t_symList *tmp;

  while (l != NULL)
    {
      tmp = l->next;
      free(l->str);
      free(l);
      l = tmp;
    }
}

t_symList	*getSymList(int	pid)
{
  t_utils_elf	*e;
  t_symList	*res;
  int		cpt;

  e = get_elf(pid);
  if (e == NULL)
    return (NULL);
  res = NULL;
  cpt = 0;
  while ((cpt * sizeof(Elf64_Sym)) < e->symtabSize)
    {
      if (!isNokSym(&e->symtab[cpt], e))
	res = addSymlist(res, &e->symtab[cpt], (char *)
			 ((unsigned long)e->strsymtab + e->symtab[cpt].st_name));
      ++cpt;
    }
  munmap(e->header, e->fileSize);
  free(e);
  return (res);
};
